/**
 * description: 公共方法-CRM
 * author: tfy
 * createdDate: 2019-09-05
*/
import { requestAPI } from "@/api/commonRequest";
// import { listenerCount } from "cluster";
export const PAGE_SIZE = 10
export const PAGE_INDEX = 1
/* 配置化接入（tableCols内操作项）
*/

export function __getConfigCol(__this, CacheConfig, operationArray, tableArray) {
  var cols = operationArray
  if (CacheConfig.cacheData[__this.$route.path] && CacheConfig.cacheData[__this.$route.path].tableCols.length > 0) {
    cols = cols.concat(CacheConfig.cacheData[__this.$route.path].tableCols)
  } else {
    cols = cols.concat(tableArray)
  }
  return cols
}
/*表单页内表单分组
*/
export function __group(array, subGroupLength) {
  let index = 0;
  let newArray = [];
  while (index < array.length) {
    newArray.push(array.slice(index, index += subGroupLength));
  }
  return newArray;
}
/*简单导出
tabledataConfig：网格配置  object
api：请求API   string
callback:回调处理   function
*/
export function exportSimpleTablequery(__this, tabledataConfig, api, name, callback) {
  if (tabledataConfig && api) {
    let queryObj = {
      apiConfig: api,
      apiServices: [
        {
          apiQueryRow: tabledataConfig.queryObj.apiQueryRow
        }
      ],
      variables: {
        pageSize: 999999,
        pageIndex: tabledataConfig.pageIndex ? tabledataConfig.pageIndex : PAGE_INDEX,
        //当前中台使用的名称有dataInfo、info，具体查看API文档
        dataInfo: tabledataConfig.queryObj.params
      }
    };
    var paramD = __this.$getParams(queryObj);
    // 导出添加额外参数
    buildExportParam(__this, tabledataConfig, paramD, '', name);
    // 中台API方法
    requestAPI(paramD).then((response) => {
      try {
        response
      } catch (error) {
        showMessages(__this, "error", error);
        return
      }
      if (response.type) {
        if (callback) {
          return callback(response, paramD.excelName)
        } else {
          __this.$utils.downloadFile(response, paramD.excelName)
        }
      } else {
        showMessages(
          __this,
          "error",
          JSON.stringify(response)
        );
      }
    })
  } else {
    // 抛出错误信息，
    showMessages(__this, "error", '网格配置项不存在或API不存在');
  }
}
/*导出添加参数
queryObj  object
paramsObj   object
type  导出文件类型
name  导出文件名称
*/
function buildExportParam(__this, tabledataConfig, paramsObj, type, name) {
  var excelCols = {}
  for (var i = 0; i < tabledataConfig.colnames.length; i++) {
    excelCols[tabledataConfig.colnames[i].value] = tabledataConfig.colnames[i].label
  }
  // 数据类型
  paramsObj.dataType = type ? type : 'excel'
  // excel文件名称
  paramsObj.excelName = name ? name : "导出"
  // 根据请求API描述excel数据对象
  paramsObj.excels = [{
    // excel sheet名称
    title: "Sheet1",
    // 对应API服务编码
    actionName: tabledataConfig.queryObj.apiConfig.ServiceCode,
    // excel列
    columns: excelCols
  }]
}
/*简单网格查询返回
tabledataConfig：网格配置  object
api：请求API   string
callback:回调处理   function
*/
export function simpleTablequery(__this, tabledataConfig, api, callback) {
  if (tabledataConfig && api) {
    let queryObj = {
      // api配置
      apiConfig: api,
      // 需要调用的API服务配置
      apiServices: [
        {
          //表格中台返回网格的字段
          apiQueryRow: tabledataConfig.queryObj.apiQueryRow
        }
      ],
      //条件/实体参数（变量），根据typeParam中的定义配置
      variables: {
        pageSize: tabledataConfig.pageSize ? tabledataConfig.pageSize : PAGE_SIZE,
        pageIndex: tabledataConfig.pageIndex ? tabledataConfig.pageIndex : PAGE_INDEX,
        //当前中台使用的名称有dataInfo、info，具体查看API文档
        dataInfo: tabledataConfig.queryObj.params
      }
    };
    //转换中台请求格式数据
    var paramD = __this.$getParams(queryObj);
    // 中台API方法
    __this.listLoading = true
    requestAPI(paramD).then(response => {
      __this.listLoading = false
      try {
        var data = response
      } catch (error) {
        showMessages(__this, "error", error);
        return
      }
      if (data.result == "1") {
        //网格配置项
        tabledataConfig.pageIndex = data.pageindex;
        tabledataConfig.pageSize = data.pagesize;
        tabledataConfig.total = data.records;
        if (data.rows.length > 0) {
          if (callback) {
            return callback(data)
          } else {
            if (tabledataConfig.tableData) {
              tabledataConfig.tableData = data.rows
            }
            if (tabledataConfig.tabledata) {
              tabledataConfig.tabledata = data.rows
            }
          }
        } else {
          // 返回数据为空
          if (tabledataConfig.tableData) {
            tabledataConfig.tableData = []
          }
          if (tabledataConfig.tabledata) {
            tabledataConfig.tabledata = []
          }

        }
      } else {
        // 抛出错误信息，后台返回报错
        tabledataConfig.tableData = []
        showMessages(
          __this,
          "error",
          data.msg
        );
      }
    })
      .catch(response => {
        __this.listLoading = false
        console.log('请求超时或者服务器异常:' + JSON.stringify(response))
        var errInfo = "请求超时或者服务器异常";
        showMessages(
          __this,
          "error",
          errInfo
        );
      });
  } else {
    // 抛出错误信息，
    showMessages(__this, "error", '网格配置项不存在或API不存在');
  }
}
/*保存提交类封装
variables：保存传参
api：请求API   string
callback:回调处理   function
*/
export function mutationSumbit(__this, variables, api, callback) {
  if (variables && api) {
    let queryObj = {
      type: "mutation",
      // api配置
      apiConfig: api,
      // 需要调用的API服务配置
      apiServices: [
        {
          //表格中台返回网格的字段
          apiQueryRow: []
        }
      ],
      //条件/实体参数（变量），根据typeParam中的定义配置
      variables
    };
    //转换中台请求格式数据
    var paramD = __this.$getParams(queryObj);
    // 中台API方法
    requestAPI(paramD).then(response => {
      try {
        var data = response
      } catch (error) {
        showMessages(__this, "error", error);
        return
      }
      if (response.result == "1") {
        showMessages(__this, "success", "成功");
        if (callback) {
          return callback()
        } else {
          return
        }
      } else {
        showMessages(
          __this,
          "error",
          data.msg,
          '',
          true
        );
      }
    })
    // .catch(response => {
    //   console.log('请求超时或者服务器异常:'+JSON.stringify(response))
    //   var errInfo = "请求超时或者服务器异常";
    //   showMessages(
    //     __this,
    //       "error",
    //       errInfo
    //     );
    // });
  } else {
    // 抛出错误信息，
    showMessages(__this, "error", '网格配置项不存在或API不存在');
  }
}
/*网格自适应
高度、
滚动条
*/
export function __setAutoTableStyle(__this) {
  let el = document.getElementsByClassName("el-table")[0].children[2];
  // let el= __this.$refs.table.$el.children[2]     // el 寻找有问题就采用此种方法
  let h = document.documentElement.clientHeight || document.body.clientHeight;
  el.style.overflowX = 'auto'    //设置底部滚动条
  // 规则：浏览器窗口高度- 40（系统登陆信息高度）-40（导航栏）-40底部页签-28*2（查询条件label、查询结果label）
  // -58（查询条件内容区域）-50（table底部分页）-33（底部遮拦住弟10条数据部分）-5（可扩展调整高度）
  el.style.height = h - (40 + 40 + 40 + 2 * 28 + 58) - 5 - 50 - 33 + 'px'
  __this.tableHeight = h - (40 + 40 + 40 + 2 * 28 + 58) - 5 - 50
}
/*列表点击跳转详情页面
name：页面名称  string
fullPath：路径   string
rowData:传递参数   object
*/
export function jumpDetailTag(__this, name, fullPath, rowData) {
  var nowViews = __this.$store.state.tagsView.visitedViews;
  var view = __this.$route
  if (rowData) {
    Object.assign(rowData, { parent: __this })
  } else {
    rowData = { parent: __this }
  }
  console.log(nowViews);
  var nowViewsLen = nowViews.length;
  for (var i = 0; i < nowViewsLen; i++) {
    if (!(nowViews[i].name == name)) {
      if (i == nowViewsLen - 1) {
        __this.$router.push({ name: name, params: rowData ? rowData : '' });
        return;
      } else {
        continue
      }
    } else {
      let nowView = nowViews[i]
      clearKeepAliveCache(__this, nowView);
      __this.$store
        .dispatch("tagsView/delView", {
          name: name,
          path: fullPath
        })
        .then(() => {
          // 清除keepAlive缓存
          __this.$router.push({ name: name, params: rowData ? rowData : '' });
        });
    }
  }
}

export function crmRegExp(type, extral) {
  switch (type) {
    case 'empty': return /\S/  //为空校验
    case 'phonePre': return /^[+][0-9]{2}|^[0-9-]{2,5}[-]$/
    case "code": return /^[a-zA-Z0-9]{1,50}$/ //50个字符以内的编码（数字或英文）
    case "English" : return /^[a-zA-Z]{1,50}$/ //50个字符以内的英文
    case "chinese": return /^[\u4e00-\u9fa5]{1,50}$/ //50个字以内的中文
    case "chnAndEn": return /^[\u4e00-\u9fa5a-zA-Z]{1,50}$/ //50个字以内的中英文
    case 'tel': return /^1(3|4|5|6|7|8|9)\d{9}$/; //手机号码
    case 'addr': return /^[\u4e00-\u9fa5\w]{1,50}$/;//中文地址
    case 'phone': return /^0\d{2,3}-\d{7,8}$/; //国内电话号码
    case 'telOrphone': return /^((0\d{2,3}-\d{7,8})|(1(3|4|5|6|7|8|9)\d{9}))$/;//手机或电话
    case 'num': return /^[0-9]*$/; //数字
    case 'notZeroNum':return /^\+?[1-9][0-9]*$/;//非零正整数
    case 'name': return /^[\u4e00-\u9fa5]{1,10}$/; //汉字
    case 'email': return /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/  //email地址
    case 'idCard': return /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/ //18位身份证号码(数字、字母x结尾)
    case 'date': return /([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/ //日期格式(年月日)
    case 'zip': return /[1-9]\d{5}(?!\d)/;//中国邮政编码(中国邮政编码为6位数字)
    case 'QQ': return /[1-9][0-9]{4,14}/ //QQ号
    case "weChat": return /^[a-zA-Z\d_]{5,}$/ //微信号
    case "ip" :return /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/ //ip地址
    case "port":return /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/ //端口号
    case "password":return /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[._~!@#$^&*])[A-Za-z0-9._~!@#$^&*]{8,20}$/ //密码校验
    case "carBand":return /^([京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][1-9DF][1-9ABCDEFGHJKLMNPQRSTUVWXYZ]\d{3}[0-9DF]|[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][\dABCDEFGHJKLNMxPQRSTUVWXYZ]{5})$/ //车牌号校验
    case "simpleName":return /^.{1,20}$/ //2-20的所有字符
    case "englishNames":return /^[a-zA-Z][a-zA-Z0-9_]*$/ //英文开头，加数字、英文大小写、下划线
    case "vin":return /^.{17,17}$/ //17位Vin码
  }
}
export function regex(__this, vaildParams) {
  let isEmpty = new RegExp(crmRegExp('empty'))
  let html = ""
  let isVaild=false
  let num=0
  for (let key in vaildParams) {
    let rex = new RegExp(crmRegExp(vaildParams[key][0]))
    if (!isEmpty.test(vaildParams[key][1]) || vaildParams[key][1] == null) {

    } else {
      if (!rex.test(vaildParams[key][1])) {
        html += vaildParams[key][2]
        isVaild=true
        num++
      }
    }
  }
  if(isVaild){
    __this.$store.dispatch('app/lockScreen')
    __this.$message({
      title: '校验',
      dangerouslyUseHTMLString:true,
      message: html,
      duration: 1000*num,
      type: 'error',
      onClose:function(){
          if (__this.$store.state.app.loading.close) {
            setTimeout(() => {
              __this.$store.state.app.loading.close()
            }, 200)
          }
      }
      });
  }
  return !isVaild
}
// export function regex(__this, type, value, msg) {
//   let rex = new RegExp(crmRegExp(type))
//   let isEmpty = new RegExp(crmRegExp('empty'))
//   if (!isEmpty.test(value)||value==null) {
//     return true
//   } else {
//     if (!rex.test(value)) {
//       __this.$notify.error({
//         title: '校验项',
//         message: msg
//       });
//     }
//     return rex.test(value)
//   }
// }
export function crmcloseView(__this, pview) {
  var view = __this.$route
  var visitedViews = __this.$store.state.tagsView.visitedViews;
  var latestView = ''
  __this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
    visitedViews.forEach(item => {
      if (pview == '' || pview == undefined) {
        latestView = visitedViews.slice(-1)[0];
      } else {
        if (item.name == pview) {
          latestView = item;
        }
      }
    });
    if (latestView == '') {
      latestView = visitedViews.slice(-1)[0];
    }
    if (latestView) {
      __this.$router.push(latestView)
    } else {
      if (view.name === 'Dashboard') {
        __this.$router.replace({ path: '/redirect' + view.fullPath })
      } else {
        __this.$router.push('/')
      }
    }
    clearKeepAliveCache(__this, view);
  })
}
//关闭当前页面  清除当前页面的keep alive的缓存
function getParnt(val) {
  var parent = val.$parent;
  var count = ""
  for (let i = 0; i < parent.$children.length; i++) {
    if (parent.$children[i].$vnode.tag.indexOf("-AppMain") > -1) {
      return parent;
    } else {
      count = "1"
    }
  }
  if (count == "1") {
    return getParnt(parent);
  }
}
// 递归
export function __getParent(_this, term, callback) {
  var parent = _this.$parent
  if (parent[term]) {
    callback(parent[term])
  } else {
    return __getParent(parent, term, callback)
  }
}

export function clearKeepAliveCache(that, view) {
  var vnode = null;
  var parent = getParnt(that)
  for (let i = 0; i < parent.$children.length; i++) {
    if (parent.$children[i].$vnode.tag.indexOf("-AppMain") > -1) {
      let isSet = false;
      if (parent.$children[i].$vnode.componentInstance) {
        for (
          let j = 0;
          j <
          parent.$children[i].$vnode.componentInstance.$children
            .length;
          j++
        ) {
          if (
            parent.$children[i].$vnode.componentInstance.$children[
              j
            ].$vnode.tag.indexOf("-breadcrumb") < 0
          ) {
            vnode =
              parent.$children[i].$vnode.componentInstance.$children[
                j
              ].$vnode.componentInstance.$vnode.parent;

            isSet = true;
            break;
          }
        }
      }
      if (isSet) {
        break;
      }
    }
  }

  if (vnode) {
    for (let i = 0; i < vnode.componentInstance.keys.length; i++) {
      let arr = vnode.componentInstance.keys[i].split("-");
      let path = arr[arr.length - 1];
      if (path === view.path) {
        delete vnode.componentInstance.cache[
          vnode.componentInstance.keys[i]
        ];
        vnode.componentInstance.keys.splice(i, 1);
        break;
      }
    }
  }
}
// 迭代一校验
/*
表单校验提示
config = {
        custName: [true, /\S/, "input", that.queryObj.params.custName, "客户名称", "不能为空"],
        contactTel: [true, /\S/, "input", that.queryObj.params.contactTel, "电话", "不能为空"],
        gender: [true, /\S/, "value",that.queryObj.params.gender, "性别", "不能为空"],
        useGenderCode: [true, /\S/, "value", that.queryObj.params.useGenderCode, "性别", "不能为空"],
        SourceOfChannel: [true, /\S/, "value", that.queryObj.params.SourceOfChannel, "渠道来源", "不能为空"],
        clueLevelCode: [true, /\S/, "value", that.queryObj.params.clueLevelCode, "意向级别", "不能为空"],
        inteSeriesCode: [true, /\S/, "value", that.queryObj.params.inteSeriesCode, "意向车系", "不能为空"],
        inteTypeCode: [true, /\S/, "value", that.queryObj.params.inteTypeCode, "意向车型", "不能为空"]
      };
*/
export function showMessages(__this, type, message, isHTML,isRequest) {

  switch (type) {
    case 'success':
        __this.$store.dispatch('app/lockScreen')
        __this.$message({
          title: '成功',
          dangerouslyUseHTMLString:isHTML ? true : false,
          message: message,
          duration: 1000,
          type: type,
          onClose:function(){
              if (__this.$store.state.app.loading.close) {
                setTimeout(() => {
                  __this.$store.state.app.loading.close()
                }, 1000)
              }
          }
          });
      break;
    case 'warning':
        __this.$store.dispatch('app/lockScreen')
      __this.$message({
        title: '警告',
        dangerouslyUseHTMLString:isHTML ? true : false,
        message: message,
        duration: 1000,
        type: type,
        onClose:function(){
            if (__this.$store.state.app.loading.close) {
              setTimeout(() => {
                __this.$store.state.app.loading.close()
              }, 200)
            }
        }
        });
      break;
    case 'warning':
        __this.$store.dispatch('app/lockScreen')
      __this.$message({
        title: '消息',
        dangerouslyUseHTMLString:isHTML ? true : false,
        message: message,
        duration: 1000,
        type: type,
        onClose:function(){
            if (__this.$store.state.app.loading.close) {
              setTimeout(() => {
                __this.$store.state.app.loading.close()
              }, 200)
            }
        }
        });
      break;
    case 'error':
        __this.$store.dispatch('app/lockScreen');
        __this.$store.dispatch('app/loading', 'lock')
        console.log(message.split('</div>'))
        __this.$message({
          title: '消息',
          dangerouslyUseHTMLString:isHTML ? true : false,
          message: message,
          // duration: message.indexOf('</div>')>-1 ? 1000*(message.split('</div>').length-1) : 1000,
          duration:isRequest||!isHTML ? 1000 : 0,
          type: type,
          onClose:function(){
              if (__this.$store.state.app.loading.close) {
                setTimeout(() => {
                  __this.$store.state.app.loading.close()
                  console.log(__this.$utils)
                }, 200)
              }
          }
          });
      break;
      default:
      break;
  }
}

export function validForm(__this, formName, config, callback, otherValid) {
  var html = ''
  // let flag = false;
  Object.keys(config).forEach(function (key) {
    let isMust = config[key][0] //是否必填
    let reg = config[key][1]    //正则表达式
    let type = config[key][2]    //表单类型
    let val = config[key][3]    //表单值
    let name = config[key][4]   //表单name
    let warn = config[key][5] ? config[key][5] : '不能为空'   //提示语句
    let events = config[key][6] //事件
    let extral = config[key][7]  //额外校验
    // message占用空格表示提示文字不在Input周边显示   { pattern: /^[0-9-_]{1,11}$/, message: '联系人手机号码的格式错误!', trigger: 'blur' };
    config[key][type] = [{ required: isMust ? true : false, message: ' ', trigger: events ? events : '' }]
    if (extral && Object.prototype.toString.call(extral) == '[object Object]') {
      config[key][type].push(extral)
    }
    else if (extral && Object.prototype.toString.call(extral) == '[object Array]') {
      config[key][type].concat(extral)
    }
    console.log(key);
    val = val ? val : ''
    if (!reg.test(val)) {
      html = html + `<div>${name}:${warn}</div>`
    }
  });
  if (html != '') {
    showMessages(__this, 'error', html, true)
    callback ? callback(false) : ''
    return
  }
  callback ? callback(true) : ''
  return
  // 拼接完后，校验
  __this.rulescust = config
  __this.$refs[formName].validate(valid => {
    console.log('valid:' + valid)
    if (valid) {
      callback ? callback(valid) : ''
    } else {
      if (html != '') {

        __this.$notify.error({
          title: "校验项",
          dangerouslyUseHTMLString: true,
          message: html
        });
        callback ? callback(false) : ''
        return
      }
      callback ? callback(true) : ''
    }
  });

}

export default {
  exportSimpleTablequery,
  simpleTablequery,
  jumpDetailTag,
  validForm,
  crmRegExp,
  regex,
  crmcloseView,
  showMessages,
  mutationSumbit,
  __setAutoTableStyle,
  __group,
  __getConfigCol,
  __getParent,
  PAGE_SIZE,
  PAGE_INDEX
}
